<?php if(!defined('BASEPATH')) exit ('Forbidden.');

class Nvl_Controller extends CI_Controller{
	/*
	 * Function to convert number to roman number
	**/
	function __numberToRoman($num) {
		$n = intval($num);
		$result = '';
		$lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
			'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
			'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
		foreach ($lookup as $roman => $value) {
			$matches = intval($n / $value);
			$result .= str_repeat($roman, $matches);
			$n = $n % $value;
		}
		return $result;
	}
	
	/*
	 * Function to convert to indonesia day format
	**/
	function __getIndonesiaDay($num){
		$dayArray = array("Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu", "Minggu");
		if(array_key_exists($num-1, $dayArray)){
			return $dayArray[$num-1];
		}else{
			return 'Undefined';
		}
	}
	
	/*
	 * Function to convert to indonesia month format
	**/
	function __getIndonesiaMonth($num){
		$monthArray = array("Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");
		if(array_key_exists($num-1, $monthArray)){
			return $monthArray[$num-1];
		}else{
			return 'Undefined';
		}
	}
	
	/*
	 * Function to convert indonesia time format
	**/
	function __getIndonesiaDate($date, $longformat=FALSE){
		if($longformat){
			$day = date('d', strtotime($date));
			$month = date('n', strtotime($date));
			$year = date('Y', strtotime($date));			
			$month = $this->__getIndonesiaMonth($month);
			return $day.' '.$month.' '.$year;
		}else{
			return date('d-m-Y', strtotime($date));
		}
	}
	
	/*
	 * Function to convert nominal to indonesia nominal text format
	**/
	function __terbilang($num){
		$digits = array(
			0 => "nol",
			1 => "satu",
			2 => "dua",
			3 => "tiga",
			4 => "empat",
			5 => "lima",
			6 => "enam",
			7 => "tujuh",
			8 => "delapan",
			9 => "sembilan");
		$orders = array(
			0 => "",
			1 => "puluh",
			2 => "ratus",
			3 => "ribu",
			6 => "juta",
			9 => "miliar",
			12 => "triliun",
			15 => "kuadriliun");

		$is_neg = $num < 0; $num = "$num";

		$int = ""; if (preg_match("/^[+-]?(\d+)/", $num, $m)) $int = $m[1];
		$mult = 0; $wint = "";

		while (preg_match('/(\d{1,3})$/', $int, $m)) {

			$s = $m[1] % 10;
			$p = ($m[1] % 100 - $s)/10;
			$r = ($m[1] - $p*10 - $s)/100;

			if ($r==0) $g = "";
			elseif ($r==1) $g = "se$orders[2]";
			else $g = $digits[$r]." $orders[2]";

			if ($p==0) {
				if ($s==0);
				elseif ($s==1) $g = ($g ? "$g ".$digits[$s] :
				($mult==0 ? $digits[1] : "se"));
				else $g = ($g ? "$g ":"") . $digits[$s];
			} elseif ($p==1) {
				if ($s==0) $g = ($g ? "$g ":"") . "se$orders[1]";
				elseif ($s==1) $g = ($g ? "$g ":"") . "sebelas";
				else $g = ($g ? "$g ":"") . $digits[$s] . " belas";
			} else {
				$g = ($g ? "$g ":"").$digits[$p]." puluh".
				($s > 0 ? " ".$digits[$s] : "");
			}

			$wint = ($g ? $g.($g=="se" ? "":" ").$orders[$mult]:"").
			($wint ? " $wint":"");

			$int = preg_replace('/\d{1,3}$/', '', $int);
			$mult+=3;
		}
		if (!$wint) $wint = $digits[0];
		$frac = ""; if (preg_match("/\.(\d+)/", $num, $m)) $frac = $m[1];
		$wfrac = "";
		for ($i=0; $i<strlen($frac); $i++) {
			$wfrac .= ($wfrac ? " ":"").$digits[substr($frac,$i,1)];
		}
		$hasil= ($is_neg ? "minus ":"").$wint.($wfrac ? " koma $wfrac":"");
		$hasil=str_replace("sejuta","satu juta",$hasil);
		return $hasil;
	}
	
}